#!/bin/bash
# Windows Process Spy - Build Script (Bash version)

# 检测操作系统类型
if [[ "$OSTYPE" == "msys" || "$OSTYPE" == "win32" ]]; then
    echo "========================================="
    echo "  检测到Windows环境"
    echo "========================================="
    echo "在此环境中，build.sh需要通过Git Bash或WSL运行。"
    echo ""
    echo "Windows用户推荐使用build.bat，这是专门为Windows环境优化的脚本。"
    echo ""
    echo "使用方法:"
    echo "1. 在PowerShell或cmd中运行: .\\build.bat"
    echo ""
    echo "或者继续使用此脚本(需要Git for Windows):"
    echo "1. 安装Git for Windows (https://gitforwindows.org/)"
    echo "2. 右键点击项目文件夹，选择\"Git Bash Here\""
    echo "3. 在Git Bash中运行: ./build.sh"
    echo "========================================="
    echo ""
fi

# 定义颜色
GREEN="\033[0;32m"
YELLOW="\033[1;33m"
RED="\033[0;31m"
NC="\033[0m" # No Color

echo -e "${GREEN}Windows Process Spy - 一站式构建工具${NC}"
echo -e "${GREEN}=================================${NC}"
echo ""

# 确保目录分隔符一致性处理并提前定义SCRIPT_DIR
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

echo -e "${YELLOW}[1/3] 正在设置UV环境...${NC}"

# 运行setup_uv.bat设置环境
SETUP_BAT="$SCRIPT_DIR/setup_uv.bat"
if [ -f "$SETUP_BAT" ]; then
    echo -e "${YELLOW}[INFO] 运行 setup_uv.bat 来设置UV环境...${NC}"
    echo -e "${YELLOW}[INFO] 使用绝对路径: $SETUP_BAT${NC}"
    
    # 检查系统环境并优化错误处理
    if [[ "$OSTYPE" == "msys" || "$OSTYPE" == "win32" ]]; then
        # 在MSYS/Git Bash环境中
        if command -v cmd.exe > /dev/null; then
            # 使用绝对路径调用批处理文件
            cmd.exe /c "$SETUP_BAT"
            if [ $? -ne 0 ]; then
                echo -e "${RED}[ERROR] UV环境设置失败！${NC}"
                echo -e "${YELLOW}[INFO] 请确保您有足够权限运行此脚本${NC}"
                echo -e "${YELLOW}[INFO] 您也可以手动运行 setup_uv.bat 后再继续${NC}"
                echo -e "${YELLOW}[提示] 在Windows环境中，建议直接运行: .\\build.bat${NC}"
                read -p "按Enter键退出..."
                exit 1
            fi
        else
            echo -e "${RED}[ERROR] 无法找到cmd.exe，请确保在正确的环境中运行${NC}"
            echo -e "${YELLOW}[提示] 在Windows环境中，建议直接运行: .\\build.bat${NC}"
            read -p "按Enter键退出..."
            exit 1
        fi
    else
        # 在非Windows环境中，尝试运行setup_uv.bat
        echo -e "${YELLOW}[WARN] 检测到非Windows环境，尝试运行setup_uv.bat${NC}"
        cmd.exe /c "$SETUP_BAT" 2>/dev/null || {
            echo -e "${YELLOW}[INFO] 非Windows环境下setup_uv.bat可能无法正常运行${NC}"
            echo -e "${YELLOW}[INFO] 请确保您有兼容层或直接在Windows环境中构建${NC}"
        }
    fi
    
    echo -e "${GREEN}[OK] UV环境设置完成${NC}"
else
    echo -e "${RED}[ERROR] setup_uv.bat 未找到！${NC}"
    echo -e "${YELLOW}[INFO] 请检查文件是否存在于: $SCRIPT_DIR${NC}"
    read -p "按Enter键退出..."
    exit 1
fi

# 优先使用虚拟环境的Python
VENV_PYTHON="$SCRIPT_DIR/.venv/Scripts/python.exe"

if [ -f "$VENV_PYTHON" ]; then
    PYTHON_CMD="$VENV_PYTHON"
    echo -e "${GREEN}[OK] 使用虚拟环境Python: $PYTHON_CMD${NC}"
else
    # 备选Python路径列表
    PYTHON_PATHS=(
        "C:/Users/lvan/AppData/Local/Programs/Python/Python313/python.exe"
        "C:/Python313/python.exe"
        "python3"
        "python"
    )
    
    PYTHON_CMD=""
    for path in "${PYTHON_PATHS[@]}"; do
        if [ -f "$path" ] || command -v "$path" > /dev/null 2>&1; then
            PYTHON_CMD="$path"
            echo -e "${YELLOW}[WARN] 虚拟环境未找到，使用系统Python: $path${NC}"
            break
        fi
    done
    
    if [ -z "$PYTHON_CMD" ]; then
        echo -e "${RED}[ERROR] 未找到可用的Python解释器${NC}"
        echo -e "${YELLOW}[INFO] 请确保Python已安装并添加到系统PATH${NC}"
        read -p "按Enter键退出..."
        exit 1
    fi
fi

# 检查Python是否存在
if [ ! -f "$PYTHON_CMD" ] && ! command -v "$PYTHON_CMD" > /dev/null 2>&1; then
    echo -e "${RED}[ERROR] Python未找到: $PYTHON_CMD${NC}"
    echo "请创建虚拟环境或检查Python安装"
    read -p "按Enter键退出..."
    exit 1
fi

echo ""
echo -e "${YELLOW}Checking Python version...${NC}"
"$PYTHON_CMD" --version
if [ $? -ne 0 ]; then
    echo -e "${RED}[ERROR] Python版本检查失败${NC}"
    read -p "按Enter键退出..."
    exit 1
fi

echo ""
echo -e "${YELLOW}[2/3] 正在执行构建过程...${NC}"
echo ""
"$PYTHON_CMD" build_universal.py

if [ $? -ne 0 ]; then
    echo ""
    echo -e "${RED}[ERROR] 构建失败!${NC}"
    read -p "按Enter键退出..."
    exit 1
fi

echo ""
echo -e "${GREEN}=====================================${NC}"
echo -e "${GREEN}[SUCCESS] 构建完成!${NC}"
echo -e "${GREEN}=====================================${NC}"
echo ""
echo "生成的文件:"
echo "- release/WindowSpyGUI.exe"
echo "- release/WindowSpyCLI.exe"
echo "- release/ (发布包)"
echo ""
echo -e "${YELLOW}注意: 文件已保存到release目录中${NC}"
read -p "按Enter键退出..."
